"""
应用配置管理
"""

from pydantic_settings import BaseSettings
from typing import Optional
import os
from pathlib import Path


class Settings(BaseSettings):
    """应用配置类"""
    
    # 基础配置
    PROJECT_NAME: str = "DataPlexus"
    VERSION: str = "1.0.0"
    DESCRIPTION: str = "数据治理平台"
    
    # API配置
    API_HOST: str = "0.0.0.0"
    API_PORT: int = 8000
    API_PREFIX: str = "/api"
    
    # 主数据库配置 - DataPlexus系统数据
    DB_HOST: str = "192.168.37.11"
    DB_PORT: int = 3306
    DB_USER: str = "root"
    DB_PASSWORD: str = "123456"
    DB_NAME: str = "data_plexus"

    # 外部数据源配置 - Dinky数据库 (仅用于解析)
    DINKY_DB_HOST: str = "192.168.37.11"
    DINKY_DB_PORT: int = 3306
    DINKY_DB_USER: str = "root"
    DINKY_DB_PASSWORD: str = "123456"
    DINKY_DB_NAME: str = "dinky"

    # Neo4j配置 - 血缘图数据库
    NEO4J_URI: str = "bolt://localhost:7687"
    NEO4J_USER: str = "neo4j"
    NEO4J_PASSWORD: str = "password"
    
    # 安全配置
    SECRET_KEY: str = "your-secret-key-here-change-in-production"
    ALGORITHM: str = "HS256"
    ACCESS_TOKEN_EXPIRE_MINUTES: int = 30
    REFRESH_TOKEN_EXPIRE_DAYS: int = 7
    EMAIL_RESET_TOKEN_EXPIRE_HOURS: int = 1

    # 用户安全配置
    MAX_LOGIN_ATTEMPTS: int = 5
    ACCOUNT_LOCK_DURATION_MINUTES: int = 15
    PASSWORD_MIN_LENGTH: int = 8
    PASSWORD_REQUIRE_UPPERCASE: bool = True
    PASSWORD_REQUIRE_LOWERCASE: bool = True
    PASSWORD_REQUIRE_DIGITS: bool = True
    PASSWORD_REQUIRE_SPECIAL: bool = True

    # 会话配置
    SESSION_EXPIRE_HOURS: int = 24
    REMEMBER_ME_EXPIRE_DAYS: int = 30
    
    # 环境配置
    ENVIRONMENT: str = "development"
    DEBUG: bool = True
    LOG_LEVEL: str = "INFO"
    LOG_FILE: str = "logs/app.log"

    # 文件存储配置
    UPLOAD_DIR: str = "uploads"
    CSV_EXPORT_DIR: str = "exports/csv"
    MAX_FILE_SIZE: int = 100 * 1024 * 1024  # 100MB
    
    @property
    def DATABASE_URL(self) -> str:
        """构建主数据库连接URL"""
        return f"mysql+pymysql://{self.DB_USER}:{self.DB_PASSWORD}@{self.DB_HOST}:{self.DB_PORT}/{self.DB_NAME}"

    @property
    def ASYNC_DATABASE_URL(self) -> str:
        """构建主数据库异步连接URL"""
        return f"mysql+aiomysql://{self.DB_USER}:{self.DB_PASSWORD}@{self.DB_HOST}:{self.DB_PORT}/{self.DB_NAME}"

    @property
    def DINKY_DATABASE_URL(self) -> str:
        """构建Dinky数据库连接URL"""
        return f"mysql+pymysql://{self.DINKY_DB_USER}:{self.DINKY_DB_PASSWORD}@{self.DINKY_DB_HOST}:{self.DINKY_DB_PORT}/{self.DINKY_DB_NAME}"

    @property
    def DINKY_ASYNC_DATABASE_URL(self) -> str:
        """构建Dinky数据库异步连接URL"""
        return f"mysql+aiomysql://{self.DINKY_DB_USER}:{self.DINKY_DB_PASSWORD}@{self.DINKY_DB_HOST}:{self.DINKY_DB_PORT}/{self.DINKY_DB_NAME}"
    
    @property
    def CSV_EXPORT_PATH(self) -> str:
        """获取CSV导出目录的绝对路径"""
        from pathlib import Path
        export_path = Path(self.CSV_EXPORT_DIR)
        export_path.mkdir(parents=True, exist_ok=True)
        return str(export_path.absolute())

    @property
    def UPLOAD_PATH(self) -> str:
        """获取上传目录的绝对路径"""
        from pathlib import Path
        upload_path = Path(self.UPLOAD_DIR)
        upload_path.mkdir(parents=True, exist_ok=True)
        return str(upload_path.absolute())

    class Config:
        env_file = ".env"
        case_sensitive = True


# 创建全局配置实例
settings = Settings()

# 确保日志目录存在
log_dir = Path(settings.LOG_FILE).parent
log_dir.mkdir(parents=True, exist_ok=True)
